<?php
class reporterModel extends appModel
{
    private $pageSize = 20;
    public function get_reporter_list()
    {
        $sql = "SELECT p1.id,p1.username,p1.created,p2.is_live FROM ".$this->table("admin")." p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.id=p2.admin_id  WHERE p1.group_id = 5 ";
        return $this->getAll($sql);
    }
    
    public function get_reporter($id)
    {
        $sql = "SELECT id,username,created FROM ".$this->table("admin")." WHERE id = $id LIMIT 1 ";
        return $this->getRow($sql);
    }
    
    public function check_info($username,$id = 0)
    {
        $sql = "SELECT count(*) FROM ".$this->table("admin")." WHERE username = '$username' AND id != '$id' LIMIT 1 ";
        $res = $this->getOne($sql);
        if($res){
            return false;
        }else{
            return true;
        }
    }
    
    public function is_reporter($admin_id)
    {
        $sql = "SELECT group_id FROM ".$this->table("admin")." WHERE id = '$admin_id' LIMIT 1  ";
        $group_id = $this->getOne($sql);
        if($group_id == 5){
            return true;
        }else{
            return false;
        }
    }
    
    public function get_reporter_info($admin_id)
    {
        $sql = "SELECT * FROM ".$this->table("reporter")." WHERE admin_id = $admin_id LIMIT 1 ";
        $reporter = $this->getRow($sql);
        if($reporter['is_live'] == 1){
            $sql = "SELECT * FROM ".$this->table("reporter_new")." WHERE reporter_id = ".$reporter['id']." AND type = 1 LIMIT 1 ";
            $new = $this->getAll($sql);
            if(empty($new)){
                $new[] = array();
            }
            $reporter['new'] = $new;
        }else{
            $sql = "SELECT * FROM ".$this->table("reporter_new")." WHERE reporter_id = ".$reporter['id']." AND type = 0 ORDER BY status desc ";
            $new = $this->getAll($sql);
            $reporter['new'] = $new;
        }
        return $reporter;
        
    }
    
    public function get_new_info($new_id)
    {
        $sql = "SELECT * FROM ".$this->table("reporter_new")." WHERE id = $new_id LIMIT 1 ";
        return $this->getRow($sql);
    }
    
    public function updateStatus($status , $id ,$report_id)
    {
        $sql = "UPDATE ".$this->table("reporter_new")." SET status = 0 WHERE  id != $id AND reporter_id = $report_id AND status = $status ";
        $this->query($sql);
    }
    
    public function listComment($page = 1,$total = array())
    {
        $where = " " ;
        $logic = "AND";
        foreach($total as $k=>$v)
        {
            if($k=="startTime"&&$v!=""){
                $where.=" p1.create_time>=".$v." ".$logic;
            }elseif ($k=="endTime"&&$v!=""){
                $where.=" p1.create_time<=".$v." ".$logic;
            }elseif($k=="comment"&&$v!=""){
                $where.=" p1.comment like '%".$v."%' ".$logic;
            }elseif($k=="nickname"&&$v!=""){
                $where.=" p2.nickname like '%".$v."%' ".$logic;
            }elseif($k=="new_id"){
                $where.=" p1.new_id = '".$v."' ".$logic;
            }elseif($k=="status"){
                if($v!="all"){
                    if($v==2)
                    {
                        $where.=" p1.status = 0 ".$logic;
                    }else{
                        $where.=" p1.status = '".$v."' ".$logic;
                    }
                     
                }
            }
        }
        $where .= " 1=1  order by  p1.create_time desc ";
        $listPage=array('data'=>'','pageLink'=>'');
        $result=$this->get_page2(" p1.id,p1.comment,p1.status,p1.create_time,p2.openid,p2.nickname ",$this->table('reporter_new_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
        $listPage['pagelink']=$this->getpagelink();
        $listPage['data']=$result;
        return $listPage;
    }
    public function listComment2($page = 1,$total = array())
    {
        $where = " " ;
        $logic = "AND";
        foreach($total as $k=>$v)
        {
            if($k=="startTime"&&$v!=""){
                $where.=" p1.create_time>=".$v." ".$logic;
            }elseif ($k=="endTime"&&$v!=""){
                $where.=" p1.create_time<=".$v." ".$logic;
            }elseif($k=="comment"&&$v!=""){
                $where.=" p1.comment like '%".$v."%' ".$logic;
            }elseif($k=="nickname"&&$v!=""){
                $where.=" p2.nickname like '%".$v."%' ".$logic;
            }elseif($k=="reporter_name"&&$v!=""){
                $where.=" p4.reporter_name like '%".$v."%' ".$logic;
            }elseif($k=="status"){
                if($v!="all"){
                    if($v==2)
                    {
                        $where.=" p1.status = 0 ".$logic;
                    }else{
                        $where.=" p1.status = '".$v."' ".$logic;
                    }
                     
                }
            }
        }
        $where .= " 1=1  order by  p1.create_time desc ";
        $listPage=array('data'=>'','pageLink'=>'');
        $result=$this->get_page2(" p1.id,p1.comment,p1.status,p1.create_time,p2.openid,p2.nickname,p3.title,p4.reporter_name ",$this->table('reporter_new_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid LEFT JOIN ".$this->table("reporter_new")." p3 ON p1.new_id = p3.id LEFT JOIN ".$this->table("reporter")." p4 ON p3.reporter_id = p4.id ",$where,$page,$this->pageSize);
        $listPage['pagelink']=$this->getpagelink();
        $listPage['data']=$result;
        return $listPage;
    }
    public function addDigest($id){
        $result=array('error'=>0,'content'=>'精选修改成功');
        $time = time();
        $sql ="SELECT status FROM ".$this->table('reporter_new_comment')." WHERE id=$id";
        $status = $this->getOne($sql);
        if($status==0){
            $sql ="UPDATE ".$this->table('reporter_new_comment')." SET status=1 WHERE id=$id";
            $content = "已添加入精选";
        }else{
            $sql ="UPDATE ".$this->table('reporter_new_comment')." SET status=0 WHERE id=$id";
            $content = "已移出精选";
        }
        $res=$this->query($sql);
        if(!$res){
            $result['error']=1;
            $result['content']='精选修改精失败';
        }else{
            $result['content'] = $content;
        }
        return $result;
    }
    
    public function addLive($id){
        $result=array('error'=>0,'content'=>'直播主播修改成功');
        $time = time();
        $sql ="SELECT is_live FROM ".$this->table('reporter')." WHERE admin_id = $id";
        $status = $this->getOne($sql);
        if($status==0){
            $sql ="UPDATE ".$this->table('reporter')." SET is_live=1 WHERE admin_id=$id";
            $content = "已修改为直播主播";
        }else{
            $sql ="UPDATE ".$this->table('reporter')." SET is_live=0 WHERE admin_id=$id";
            $content = "已移出直播主播";
        }
        $res=$this->query($sql);
        if(!$res){
            $result['error']=1;
            $result['content']='直播主播修改精失败';
        }else{
            $sql = "UPDATE ".$this->table('reporter')." SET is_live=0 WHERE admin_id!=$id";
            $this->query($sql);
            $result['content'] = $content;
        }
        return $result;
    }
    
    public function get_order()
    {
        $sql = " SELECT reporter_place FROM ".$this->table('block_content')." WHERE id = 76 LIMIT 1 ";
        $block['ordering'] = $this->getOne($sql);
        $block['reporter_name'] = "插入模块";
        $block['id'] = "cha";
        $sql = "SELECT id,reporter_name,ordering,gratuity_url FROM ".$this->table("reporter")." WHERE reporter_name != '' AND ordering > 0  ORDER BY ordering ASC ";//显示的
        $showReporters = $this->getAll($sql);
        $i = 1;
        $isPush = false;
        $reporters = array();
        foreach($showReporters as  $k => $v)
        {
            if($block['ordering']>0){
              if($i==$block['ordering']){
                  $isPush = true;
                  $reporters[] = $block;
              }
            }
          $reporters[] = $v;  
            $i++;
        }
        if(!$isPush)
        {
            $reporters[] = $block;
        }
        $sql = "SELECT id,reporter_name,ordering,gratuity_url FROM ".$this->table("reporter")." WHERE reporter_name != '' AND ordering = 0  ORDER BY ordering ASC ";//显示的
        $hideReporters = $this->getAll($sql);
        foreach($hideReporters as  $k => $v)
        {
            $reporters[] = $v;
        }
        return $reporters;
    }
    
    public function get_news($page,$total)
    {
        $where = " " ;
        $logic = "AND";
        foreach($total as $k=>$v)
        {
            if($k=="reporter_name"&&$v!=""){
                $where.=" p2.reporter_name like '%".$v."%' ".$logic;
            }
        }
        $where.=" 1=1 ORDER BY p1.id DESC ";
        $result=$this->get_page2(" p1.id,p1.title,p1.comment_num,p1.like_num,p1.status,p2.reporter_name ",$this->table("reporter_new")." p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id ",$where,$page,20);
        foreach ($result as $k=>$v)
        {
            $result[$k]["uvpv"] = $this->getAllUVPV(1,$v['id'],$total);
        }
        $listPage['pagelink']=$this->getpagelink();
        $listPage['data']=$result;
        
        return $listPage;
        
    }
    
    public function getAllUVPV($type,$sid,$cfg=array())
    {
        $where = " " ;
        $logic = "AND";
        foreach ($cfg as $k => $v)
        {
            if($k=="startTime"&&$v!=""){
                $where.=" time>=".$v." ".$logic;
            }elseif ($k=="endTime"&&$v!=""){
                $where.=" time<=".$v." ".$logic;
            }
        }
        //总pv
        $sql = "SELECT SUM(number) FROM ".$this->table('reporter_uvpv')." WHERE $where type = $type AND sid = $sid ";
        $uvpv['pv'] = $this->getOne($sql);
        $sql = "SELECT COUNT(*) FROM ".$this->table("reporter_uvpv")." WHERE $where type = $type AND sid = $sid ";
        $uvpv['uv'] = $this->getOne($sql);
        return $uvpv;
    }
    
    public function getUVPV($sid,$page=1,$sort_by="time",$sort_order="DESC",$limit = 30)
    {
        $type = 1;
        $where = " 1=1 AND type = $type AND sid = $sid GROUP BY time,sid  ";
        $where.=" ORDER by ".$sort_by." ".$sort_order;
        $result=$this->get_page2(" sum(number) as pv,count(*) as uv,time ",$this->table('reporter_uvpv'),$where,$page,$limit);
        foreach($result as $k=>$v)
        {
             
        }
        $list['page']=$this->getpagelink();
        $list['list']=$result;
        return $list;
    }
    
    public function getExportUVPV($sid,$sort_by="time",$sort_order="DESC")
    {
        $where = " 1=1 AND type = 1 AND sid = $sid GROUP BY time  ";
        $where.=" ORDER by ".$sort_by." ".$sort_order;
        $sql = "SELECT time,sum(number) as pv ,count(*) as uv FROM ".$this->table("reporter_uvpv")." WHERE $where ";
        $result = $this->getAll($sql);
        foreach ($result as $k=>$v){
            $result[$k]['time'] = date("Y-m-d",$v['time']);
        }
        return $result;
         
    }
    
    public function getExport($reporter_name,$arr,$uvpv)
    {
        $result = array();
        $sql = "SELECT p1.id,p1.title,p2.reporter_name FROM ".$this->table("reporter_new")." p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id WHERE p2.reporter_name like '%$reporter_name%' ";
        $news = $this->getAll($sql);
        foreach ($news as $k=>$v)
        {
            $new = array($v['title'],$v['reporter_name']);
            for($i=0;$i<count($arr);$i++)
            {
                $uvpv = $this->getUVPVBytime($arr[$i], 1, $v['id']);
                $new[] = $uvpv=="1"?$uvpv['uv']:$uvpv['pv'];
            }
            $result[] = $new;
        }
        return $result;

	}
	
	public function getUVPVBytime($time,$type,$sid)
	{
	    $sql = "SELECT SUM(number) FROM ".$this->table('reporter_uvpv')." WHERE type = $type AND sid = $sid AND time = $time ";
	    $pv = $this->getOne($sql);
	    $sql = "SELECT COUNT(*) FROM ".$this->table("reporter_uvpv")." WHERE type = $type AND sid = $sid AND time = $time ";
	    $uv = $this->getOne($sql);
	    $uvpv['pv'] = isset($pv)?$pv:0;
	    $uvpv['uv'] = isset($uv)?$uv:0;
	    return $uvpv;
	}
	
	public function getLiveReporter($reporter_id)
	{
	    $sql = "SELECT is_live FROM ".$this->table("reporter")." WHERE id = $reporter_id LIMIT 1 ";
	    return $this->getOne($sql);
	}
}